#include "ScreenshotSettingsWidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QComboBox>
#include <QSpinBox>
#include <QPushButton>
#include <QFileDialog>
#include <QLabel>
#include "ConfigService.h"

ScreenshotSettingsWidget::ScreenshotSettingsWidget(QWidget* parent)
    : QWidget(parent)
{
    auto* layout = new QVBoxLayout(this);
    layout->setContentsMargins(16, 16, 16, 16);
    layout->setSpacing(12);

    chkAutoCopy = new QCheckBox(tr("自动复制到剪贴板"), this);
    chkAutoSave = new QCheckBox(tr("完成时自动保存"), this);

    auto* pathRow = new QHBoxLayout();
    editPath = new QLineEdit(this);
    btnBrowse = new QPushButton(tr("浏览..."), this);
    pathRow->addWidget(new QLabel(tr("保存目录:"), this));
    pathRow->addWidget(editPath, 1);
    pathRow->addWidget(btnBrowse);

    auto* nameRow = new QHBoxLayout();
    editFileName = new QLineEdit(this);
    nameRow->addWidget(new QLabel(tr("文件名模板:"), this));
    nameRow->addWidget(editFileName, 1);
    nameRow->addWidget(new QLabel(tr("使用 %1 代表时间戳"), this));

    auto* fmtRow = new QHBoxLayout();
    comboFormat = new QComboBox(this);
    comboFormat->addItem("png");
    comboFormat->addItem("jpg");
    spinJpegQuality = new QSpinBox(this);
    spinJpegQuality->setRange(0, 100);
    fmtRow->addWidget(new QLabel(tr("格式:"), this));
    fmtRow->addWidget(comboFormat);
    fmtRow->addSpacing(16);
    fmtRow->addWidget(new QLabel(tr("JPEG质量:"), this));
    fmtRow->addWidget(spinJpegQuality);
    fmtRow->addStretch();

    btnApply = new QPushButton(tr("应用"), this);

    layout->addWidget(chkAutoCopy);
    layout->addWidget(chkAutoSave);
    layout->addLayout(pathRow);
    layout->addLayout(nameRow);
    layout->addLayout(fmtRow);
    layout->addStretch();
    layout->addWidget(btnApply);

    connect(btnBrowse, &QPushButton::clicked, this, &ScreenshotSettingsWidget::onBrowsePath);
    connect(btnApply, &QPushButton::clicked, this, &ScreenshotSettingsWidget::onApply);
    connect(comboFormat, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ScreenshotSettingsWidget::onFormatChanged);

    loadFromConfig();
}

void ScreenshotSettingsWidget::loadFromConfig()
{
    auto cfg = ConfigService::instance().getScreenshotConfig();
    chkAutoCopy->setChecked(cfg.isAutoCopyToClipboard());
    chkAutoSave->setChecked(cfg.isAutoSave());
    editPath->setText(cfg.getDefaultSavePath());
    editFileName->setText(cfg.getDefaultFileName());
    int idx = comboFormat->findText(cfg.getDefaultFormat());
    if (idx >= 0) comboFormat->setCurrentIndex(idx);
    spinJpegQuality->setValue(cfg.getJpegQuality());
    onFormatChanged(comboFormat->currentIndex());
}

void ScreenshotSettingsWidget::onBrowsePath()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("选择保存目录"), editPath->text());
    if (!dir.isEmpty()) {
        editPath->setText(dir);
    }
}

void ScreenshotSettingsWidget::onApply()
{
    ScreenshotConfig cfg;
    cfg.setAutoCopyToClipboard(chkAutoCopy->isChecked());
    cfg.setAutoSave(chkAutoSave->isChecked());
    cfg.setDefaultSavePath(editPath->text());
    cfg.setDefaultFileName(editFileName->text());
    cfg.setDefaultFormat(comboFormat->currentText());
    cfg.setJpegQuality(spinJpegQuality->value());

    if (!cfg.isValid()) {
        // 简单兜底：若无效，忽略保存目录创建失败等，仍保存其它项
    }
    ConfigService::instance().setScreenshotConfig(cfg);
}

void ScreenshotSettingsWidget::onFormatChanged(int)
{
    bool isJpg = (comboFormat->currentText().toLower() == "jpg");
    spinJpegQuality->setEnabled(isJpg);
}

